package generic;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.jms.JMSException;
import javax.jms.TextMessage;

public class SubscriberClient implements Subscriber {

	private StringBuilder builder;
	long lastReceiveTime;
	private NumberFormat nfId = NumberFormat.getNumberInstance(Locale.GERMAN);
	private NumberFormat nfTime = NumberFormat.getNumberInstance(Locale.GERMAN);

	public SubscriberClient() {
		super();
		builder = new StringBuilder();
		nfId.setMinimumIntegerDigits( 3 );
		nfTime.setMinimumFractionDigits( 3 );
	}
	
	public void receive(String message) {
		long receiveTime = System.nanoTime();
		String[] values = message.split(";", 3);		
		int messsageId = Integer.valueOf(values[0]);
		long sendTime = Long.valueOf(values[1]);
		
		// write to log
    	if (messsageId == 1) {
    		builder.append("MessageID;MessageDelay");
	    	builder.append(System.lineSeparator());
    	}
    	
    	
		builder.append(nfId.format(messsageId)).append(";");
		builder.append(nfTime.format((double)(receiveTime - sendTime) / 1000000));
		builder.append(System.lineSeparator());

		// print log
		if (messsageId % 10 == 0) {
    		System.out.print(builder.toString());
    		builder.delete(0, builder.length());
    	}		
    }
	
}
